package com.movitech.mobile.cz.base.supplier.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.aop.ServiceThrowable;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.PageCode;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.supplier.bizvo.SupplierManualListRequestVO;
import com.movitech.mobile.cz.base.supplier.entity.SSupplierManual;
import com.movitech.mobile.cz.base.supplier.entity.ext.ExtSSupplierManual;
import com.movitech.mobile.cz.base.supplier.mapper.SSupplierManualMapper;
import com.movitech.mobile.cz.base.supplier.mapper.ext.ExtSSupplierManualMapper;
import com.movitech.mobile.cz.base.supplier.service.SSupplierManualService;
import com.movitech.mobile.cz.base.syndata.SynDataService;
import com.movitech.mobile.cz.base.sys.service.SysAreaService;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 非三个五供应商 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-05-11
 */
@Service
public class SSupplierManualServiceImpl extends ServiceImpl<SSupplierManualMapper, SSupplierManual> implements SSupplierManualService {

	@Autowired
	private ExtSSupplierManualMapper extSSupplierManualMapper;

	@Autowired
	private SynDataService synDataService;

	@Autowired
    private SysAreaService sysAreaService;

	private static final Logger logger = Logger.getLogger(SSupplierManualServiceImpl.class);

	/**
	 * 非三个五供应商列表查询接口
	 */
	@Override
	public RestReturnResult searchSupplierManualList(SupplierManualListRequestVO vo) {

		PageCode pageCode = new PageCode(vo.getPageNumber(), vo.getPageSize());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("scName", vo.getScName());

		pageCode.setCount(extSSupplierManualMapper.searchSupplierManualListCounts(map));

		map.put("currentRecordNum", (pageCode.getPageNo() - 1) * pageCode.getPageSize());
        map.put("pageSize", pageCode.getPageSize());
        map.put("isPage", vo.getPageFlag());

        List<ExtSSupplierManual> ls = extSSupplierManualMapper.searchSupplierManualList(map);
        for(ExtSSupplierManual obj : ls){
        	if(StringUtils.isNotEmpty(obj.getScArea())){
				String areaNames = sysAreaService.getSysAreaNamesByIds(obj.getScArea());

				if(StringUtils.isNotEmpty(areaNames)){
					obj.setScAreaName(areaNames);
				}
			}
        }

        pageCode.setList(ls);

		return new RestReturnResult(CommonConstants.SUCCESS, pageCode);
	}

	/**
	 * 非三个五供应商删除接口
	 */
	@Override
	public RestReturnResult updateSupplierManualForDel(String id) {

		try{

			if(StringUtils.isNotBlank(id)){

				SSupplierManual sSupplierManualDB = baseMapper.selectById(id);
				if(sSupplierManualDB != null){

					sSupplierManualDB.setBaseFeilds(UserUtils.getCurrentUserId(), null, CommonConstants.YES_INTEGER, false);
					int i = baseMapper.updateById(sSupplierManualDB);

					if(i > 0){
						return new RestReturnResult(CommonConstants.SUCCESS, "删除成功");
					}

					throw new RuntimeException("删除失败", new ServiceThrowable());

				}

				throw new RuntimeException("数据不存在", new ServiceThrowable());

			}

			throw new RuntimeException("传参为空", new ServiceThrowable());

		}catch(Exception e){
			e.printStackTrace();
			logger.error("================updateSupplierManualForDel========e:=="+e.getMessage());
			throw new RuntimeException(e.getMessage(), e.getCause());
		}

	}

	/**
	 * 非三个五供应商新增修改接口
	 */
	@Override
	public RestReturnResult saveOrUpdateSupplierManual(SSupplierManual sSupplierManual) {

		try{

			if(sSupplierManual != null && StringUtils.isNotBlank(sSupplierManual.getScName())){

				if(StringUtils.isNotBlank(sSupplierManual.getId())){//update

					SSupplierManual sSupplierManualDB = baseMapper.selectById(sSupplierManual.getId());
					if(sSupplierManualDB != null){

						sSupplierManual.setBaseFeilds(UserUtils.getCurrentUserId(), null, CommonConstants.NO_INTEGER, false);
						int i = baseMapper.updateById(sSupplierManual);

						if(i > 0){

							//同步供应商数据到旧系统
//							sSuppliersSubmitOldSysInterfaceService.abutmentSuplierData(sSupplierManual, false);
//							sSuppliersSubmitOldSysInterfaceService.abutmentSuplierData2MDM(sSupplierManual, false);

							return new RestReturnResult(CommonConstants.SUCCESS, "修改成功");
						}
						throw new RuntimeException("修改失败", new ServiceThrowable());

					}
					throw new RuntimeException("数据不存在", new ServiceThrowable());

				}else{//insert

//					boolean flag = synDataService.checkSupplierLicenseNo(sSupplierManual.getLicenseNo());
//					if(!flag){
//						throw new RuntimeException("该供应商已经存在", new ServiceThrowable());
//					}

					sSupplierManual.setScCode(System.currentTimeMillis() + "");
					sSupplierManual.setBaseFeilds(UserUtils.getCurrentUserId(), null, CommonConstants.NO_INTEGER, true);
					int i = baseMapper.insert(sSupplierManual);

					if(i > 0){

						//同步供应商数据到旧系统
//						sSuppliersSubmitOldSysInterfaceService.abutmentSuplierData(sSupplierManual, true);

						return new RestReturnResult(CommonConstants.SUCCESS, "新增成功");
					}
					throw new RuntimeException("新增失败", new ServiceThrowable());

				}

			}

			throw new RuntimeException("传参为空/供应商名称为空", new ServiceThrowable());

		}catch(Exception e){
			e.printStackTrace();
			logger.error("================saveOrUpdateSupplierManual========e:=="+e.getMessage());
			throw new RuntimeException(e.getMessage(), e.getCause());
		}

	}

}
